﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.IO;
using System.Threading;
using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

using CPXMenu;
using CPXMenuEditor.Controls;

namespace CPXMenuEditor.Forms
{
    public partial class ContentPicker : Form
    {
        string selectedContent;
        Type contentType;
        IServiceProvider services;
        string contentDirectory;
        ContentManager contentManager;
        IContentOwner contentOwner;
        ImportedContentDirectory selectedDirectory = null;

        [Browsable(false)]
        public string SelectedContentFile
        {
            get { return selectedContent; }
            set 
            {
                string oldContent = selectedContent;
                selectedContent = value;

                if (selectedContent != oldContent)
                {
                    contentPreview.SelectedContent = null;
                    contentManager.Unload();

                    if (!string.IsNullOrEmpty(selectedContent))
                    {
                        contentPreview.SelectedContent = contentManager.Load<object>(selectedContent);
                        okButton.Enabled = true;
                    }
                    else
                        okButton.Enabled = false;

                    contentPathLabel.Text = "Selected Content File: " + selectedContent;
                }
            }
        }

        public ContentPicker(string value, Type contentType, IServiceProvider services, string contentDirectory, IContentOwner contentOwner)
        {
            InitializeComponent();

            this.selectedContent = value;
            this.contentType = contentType;
            this.services = services;
            this.contentDirectory = contentDirectory;
            contentManager = new ContentManager(services, contentDirectory);
            contentPreview.GraphicsDevice = ((IGraphicsDeviceService)services.GetService(typeof(IGraphicsDeviceService))).GraphicsDevice;
            this.contentOwner = contentOwner;
        }


        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            if (selectedContent.Contains(MainFrm.DefaultContentDirectory))
                selectedContent = selectedContent.Substring(MainFrm.DefaultContentDirectory.Length + 1);
            else if (selectedContent.Contains(MenuProject.ImportedContentDirectoryName))
                selectedContent = selectedContent.Substring(MenuProject.ImportedContentDirectoryName.Length + 1);

            defaultContent.Initialize(selectedContent, contentType, services, MainFrm.TempDefaultContentDirectory, contentOwner.OwnerItem.GetParentProject());
            importedContent.Initialize(selectedContent, contentType, contentOwner.OwnerItem.GetParentProject());            
        }        

        private void importButton_Click(object sender, EventArgs e)
        {
            // importedContent.Import();
        }

        private void defaultContent_SelectedContentChanged(object sender, EventArgs e)
        {
            if (defaultContent.SelectedContentFile != null)
                SelectedContentFile = MainFrm.DefaultContentDirectory + "\\" + defaultContent.SelectedContentFile.Path;
            else
                SelectedContentFile = string.Empty;
        }

        private void importedContent_LoadingComplete(object sender, EventArgs e)
        {
            importButton.Enabled = true;
        }

        private void importedContent_SelectedContentChanged(object sender, EventArgs e)
        {
            if (importedContent.SelectedContentItem != null)
                SelectedContentFile = importedContent.SelectedContentItem.Path;
            else
                SelectedContentFile = string.Empty;
        }

        private void importedContent_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (importedContent.SelectedNode is ImportedContentDirectory)
                selectedDirectory = importedContent.SelectedNode as ImportedContentDirectory;
        }
    }

    
}
